/**
 * 
 */
package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Account;

// TODO: Auto-generated Javadoc
/**
 * The Class AccountDaoImpl.
 *
 * @author Hisham
 */
@Repository
public class AccountDaoImpl extends BasicDaoImpl<Account>{
	
	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;
	
	/**
	 * Gets the all account by company.
	 *
	 * @param search the search
	 * @param companyId the company id
	 * @param index the index
	 * @param count the count
	 * @param column the column
	 * @param sortDir the sort dir
	 * @return the all account by company
	 */
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByCompany(String search,Integer companyId,Integer index,Integer count,String column,String sortDir) {
		Session session =sessionFactory.getCurrentSession();
		List<Account> accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","phone","mobile","companyName","accountCreator.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.company.id").matching(companyId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class).setFirstResult(index).setMaxResults(count);
            boolean dir = false;
            if(sortDir.equals("desc")){
            	dir = true;
            }
            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
            hibernateuery.setSort(sort);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		else{
			Query query = session.createQuery("from Account  where isDeleted= :isDeleted and " +
					"accountCreator.company.id = :companyId order by "+column+" "+sortDir);
			query.setParameter("isDeleted", false);
			query.setParameter("companyId", companyId);
			query.setFirstResult(index);
			query.setMaxResults(count);
			accounts=query.list();
		}
		return  accounts;
	}
	
	/**
	 * Gets the all account by employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @param index the index
	 * @param count the count
	 * @param column the column
	 * @param sortDir the sort dir
	 * @return the all account by employee
	 */
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByEmployee(String search,Integer empId,Integer index,Integer count,String column,String sortDir) {
		Session session =sessionFactory.getCurrentSession();
		List<Account> accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","phone","mobile","companyName","accountCreator.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.id").matching(empId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class).setFirstResult(index).setMaxResults(count);
            boolean dir = false;
            if(sortDir.equals("desc")){
            	dir = true;
            }
            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
            hibernateuery.setSort(sort);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		else{
			Query query = session.createQuery("from Account  where isDeleted= :isDeleted and " +
					"accountCreator.id = :empId order by  "+column+" "+sortDir);
			query.setParameter("isDeleted", false);
			query.setParameter("empId", empId);
			query.setFirstResult(index);
			query.setMaxResults(count);
			accounts=query.list();
		}
		return  accounts;
	}
	
	/**
	 * Gets the all size for owner.
	 *
	 * @param search the search
	 * @param companyName the company name
	 * @return the all size for owner
	 */
	@SuppressWarnings("unchecked")
	public Integer getAllSizeForOwner(String search,String companyName) {
		Session session = sessionFactory.getCurrentSession();
		List<Account>accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","phone","mobile","companyName","accountCreator.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.company.companyName").matching(companyName).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		else{
			accounts=session.createQuery("from Account where isDeleted= :isDeleted and accountCreator.company.companyName=:companyName")
					.setParameter("isDeleted", false)
					.setParameter("companyName", companyName).list();
		}
		if(accounts.size()>0){
			return  accounts.size();
		}
		else{
			return 0;
		}
	}
	
	/**
	 * Gets the all size for employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @return the all size for employee
	 */
	@SuppressWarnings("unchecked")
	public Integer getAllSizeForEmployee(String search,Integer empId) {
		Session session = sessionFactory.getCurrentSession();
		List<Account>accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","phone","mobile","companyName","accountCreator.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.id").matching(empId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		accounts=session.createQuery("from Account  where isDeleted= :isDeleted and accountCreator.id = :empId")
				.setParameter("isDeleted", false)
				.setParameter("empId", empId).list();
		if(accounts.size()>0){
			return  accounts.size();
		}
		else{
			return 0;
		}
	}
	
	/**
	 * Gets the all accounts for company.
	 *
	 * @param search the search
	 * @param companyId the company id
	 * @return the all accounts for company
	 */
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountsForCompany(String search,Integer companyId) {
		Session session = sessionFactory.getCurrentSession();
		List<Account>accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","companyName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.company.id").matching(companyId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class);
            org.apache.lucene.search.Sort sort = new Sort(new SortField("dateCreated", SortField.STRING,true));
            hibernateuery.setSort(sort);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<contacts accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		else{
			accounts=session.createQuery("from Account where isDeleted= :isDeleted and accountCreator.company.id=:companyId")
					.setParameter("isDeleted", false)
					.setParameter("companyName", companyId).list();
		}
		if(accounts.size()>0){
			return  accounts;
		}
		else{
			return new ArrayList<Account>();
		}
	}
	
	/**
	 * Gets the all accounts for employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @return the all accounts for employee
	 */
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountsForEmployee(String search,Integer empId) {
		Session session = sessionFactory.getCurrentSession();
		List<Account>accounts = new ArrayList<Account>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"accountName","companyName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Account.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("accountCreator.id").matching(empId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Account.class);
            org.apache.lucene.search.Sort sort = new Sort(new SortField("dateCreated", SortField.STRING,true));
            hibernateuery.setSort(sort);
            accounts = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<accounts size>>>>>>>>>>>>>>>"+accounts.size());
		}
		else{
			accounts=session.createQuery("from Account  where isDeleted= :isDeleted and accountCreator.id = :empId")
					.setParameter("isDeleted", false)
					.setParameter("empId", empId).list();
		}
		if(accounts.size()>0){
			return  accounts;
		}
		else{
			return new ArrayList<Account>();
		}
	}
	
	/**
	 * Check company.
	 *
	 * @param companyName the company name
	 * @param leadId the lead id
	 * @return true, if successful
	 */
	@SuppressWarnings("unchecked")
	public boolean checkCompany(String companyName,Integer leadId){
		Session session = sessionFactory.getCurrentSession();
		List<Account>leads = new ArrayList<Account>();
		if(leadId!=0){
			leads=session.createQuery("from Account where isDeleted= :isDeleted and companyName=:companyName and id!=:leadId")
					.setParameter("leadId", leadId)
					.setParameter("isDeleted", false)
					.setParameter("companyName", companyName).list();
		}
		else{
			leads=session.createQuery("from Account where isDeleted= :isDeleted and companyName=:companyName")
					.setParameter("isDeleted", false)
					.setParameter("companyName", companyName).list();
		}
		if(leads.size()>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * Check phone.
	 *
	 * @param phone the phone
	 * @param leadId the lead id
	 * @return true, if successful
	 */
	@SuppressWarnings("unchecked")
	public boolean checkPhone(String phone,Integer leadId){
		Session session = sessionFactory.getCurrentSession();
		List<Account>leads = new ArrayList<Account>();
		if(leadId!=0){
			leads=session.createQuery("from Account where isDeleted= :isDeleted and phone=:phone and id!=:leadId")
					.setParameter("leadId", leadId)
					.setParameter("isDeleted", false)
					.setParameter("phone", phone).list();
		}
		else{
			leads=session.createQuery("from Account where isDeleted= :isDeleted and phone=:phone")
					.setParameter("isDeleted", false)
					.setParameter("phone", phone).list();
		}
		if(leads.size()>0){
			return true;
		}
		else{
			return false;
		}
	}
}
